home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_351 / pdc / libsrc.lzh / LibSrc / Startup / acrt0.asm next >
Assembly Source File  |  1990-04-19  |  14KB  |  484 lines

  1. * ==========================
  2. * amiga.dev/programs #12, from cscheppner, Tue Feb 24 17:54:45 1987
  3. * --------------------------
  4. * TITLE:  TWstartup.asm --- Modified Astartup, opens Amiga stdio under WB
  5. *******************************************************************
  6. * TWStartup.asm  - Amiga Startup opens stdio window on WB startup *
  7. *                  using TWspec defined in application            *
  8. *       example:   char TWspec[] = "CON:30/30/200/150/MyProgram"; *
  9. *                   ( OR  char TWspec = "";  for no window )      *
  10. *                                                                 *
  11. * Modified by C. Scheppner (08/03/86) from the following source:  *
  12. *******************************************************************
  13. *                                                                 *
  14. *   C Program Startup/Exit (Combo Version: CLI and WorkBench)     *
  15. *                                                                 *
  16. *******************************************************************
  17. *                                                                 *
  18. * Modified by Paul Petersen and Lionel Hummel to use with PDC 3.3 *
  19. *                                                                 *
  20. *******************************************************************
  21.  
  22. ******** Included Files *************************************************
  23.  
  24.                INCLUDE "exec/types.i"
  25.                INCLUDE "exec/alerts.i"
  26.                INCLUDE "exec/nodes.i"
  27.                INCLUDE "exec/lists.i"
  28.                INCLUDE "exec/ports.i"
  29.                INCLUDE "exec/libraries.i"
  30.                INCLUDE "exec/tasks.i"
  31.                INCLUDE "libraries/dos.i"
  32.                INCLUDE "libraries/dosextens.i"
  33.                INCLUDE "workbench/startup.i"
  34.  
  35. ******** Imported *******************************************************
  36.  
  37. xlib           macro
  38.                xref    _LVO\1
  39.                endm
  40.  
  41.                xref    _Input
  42.                xref    _Output
  43.  
  44.                xref    __main       ; C code entry point
  45.                xref    _TWspec      ; CON: spec in application for WB
  46.                                     ;      stdio window
  47.  
  48.                xlib    Alert
  49.                xlib    FindTask
  50.                xlib    Forbid
  51.                xlib    GetMsg
  52.                xlib    OpenLibrary
  53.                xlib    CloseLibrary
  54.                xlib    ReplyMsg
  55.                xlib    Wait
  56.                xlib    WaitPort
  57.  
  58.                xlib    CurrentDir
  59.                xlib    Open
  60.                xlib    Close
  61.  
  62. ******** Exported *******************************************************
  63.  
  64.                xdef    _SysBase
  65.                xdef    _DOSBase
  66.                xdef    _WBenchMsg
  67.  
  68.                xdef    __initialSP
  69.                xdef    __dosCmdLen
  70.                xdef    __dosCmdBuf
  71.  
  72.                xdef    __fh_stdin     ; AmigaDOS BPTR for stdin
  73.                xdef    __fh_stdout    ; AmigaDOS BPTR for stdout
  74.                xdef    __fh_stderr    ; AmigaDOS BPTR for stderr
  75.  
  76.                xdef    __exit         ; standard C exit function
  77.  
  78.  
  79. callsys        macro
  80.                CALLLIB _LVO\1
  81.                endm
  82.  
  83.  
  84. ************************************************************************
  85. *
  86. *   Standard Program Entry Point
  87. *
  88. ************************************************************************
  89. *
  90. *   main (argc, argv)
  91. *      int  argc;
  92. *      char *argv[];
  93. *
  94. ************************************************************************
  95.  
  96.                SECTION  1,CODE
  97.  
  98. startup:            ; reference for Wack users
  99.  
  100.                move.l  sp,__initialSP   ; initial task stack pointer
  101.                move.l  d0,__dosCmdLen
  102.                move.l  a0,__dosCmdBuf
  103.  
  104.       ;------ get Exec's library base pointer:
  105.  
  106.                move.l  4,a6
  107.                move.l  a6,_SysBase
  108.  
  109.       ;------ get the address of our task:
  110.  
  111.                suba.l  a1,a1
  112.                callsys FindTask
  113.                move.l  d0,a4
  114.  
  115.       ;------ are we running as a son of Workbench?
  116.  
  117.                tst.l   pr_CLI(A4)
  118.                beq     fromWorkbench
  119.  
  120. ;=======================================================================
  121. ;====== CLI Startup Code ===============================================
  122. ;=======================================================================
  123.  
  124. fromCLI:
  125.  
  126.       ;------   attempt to open DOS library:
  127.  
  128.                bsr   openDOS
  129.  
  130.       ;------ find command name:
  131.  
  132.                suba.l  a0,a0
  133.                move.l  pr_CLI(a4),d0
  134.                lsl.l   #2,d0                       ; bcpl pointer conversion
  135.                move.l  cli_CommandName(a0,d0.l),d0
  136.                lsl.l   #2,d0                       ; bcpl pointer conversion
  137.  
  138.       ;------ create buffer and array:
  139.  
  140.                movem.l a2/a3,-(sp)
  141.                lea     argvBuffer,a2
  142.                lea     argvArray,a3
  143.  
  144.       ;------ fetch command name:
  145.  
  146.                move.l  d0,a0
  147.                moveq.l #0,d0
  148.                move.b  (a0)+,d0        ; size of command name
  149.                clr.b   0(a0,d0.l)      ; terminate the string
  150.                move.l  a0,(a3)+
  151.  
  152.       ;------   collect parameters:
  153.  
  154.                move.l  __dosCmdLen,d0
  155.                move.l  __dosCmdBuf,a0
  156.  
  157.       ;------ null terminate the string, eat trailing garbage
  158.  
  159.                lea     0(a0,d0.l),a1
  160. stripjunk:
  161.                cmp.b   #' ',-(a1)
  162.  
  163.                dbhi    d0,stripjunk
  164.                clr.b   1(a1)
  165. newarg:
  166.  
  167.       ;------ skip spaces
  168.  
  169.                move.b  (a0)+,d1
  170.                beq.s   parmExit
  171.                cmp.b   #' ',d1         ; space
  172.                beq.s   newarg
  173.                cmp.b   #9,d1           ; tab
  174.                beq.s   newarg
  175.  
  176.       ;------ push address of the next parameter
  177.  
  178.                move.l  a2,(a3)+
  179.  
  180.       ;------ process quotes
  181.  
  182.                cmp.b   #'"',d1
  183.                beq.s   doquote
  184.  
  185.       ;------ copy the parameter in
  186.  
  187.                move.b  d1,(a2)+
  188. nextchar:
  189.  
  190.       ;------ null termination check
  191.  
  192.                move.b  (a0)+,d1
  193.                beq.s   parmExit
  194.                cmp.b   #' ',d1
  195.                beq.s   endarg
  196.  
  197.                move.b  d1,(a2)+
  198.                bra.s   nextchar
  199. endarg:
  200.                clr.b   (a2)+
  201.                bra.s   newarg
  202. doquote:
  203.  
  204.       ;------ process quoted strings
  205.  
  206.                move.b  (a0)+,d1
  207.                beq.s   parmExit
  208.                cmp.b   #'"',d1
  209.                beq.s   endarg
  210.  
  211.       ;------ '*' is the BCPL escape character
  212.  
  213.                cmp.b   #'*',d1
  214.                bne.s   addquotechar
  215.  
  216.                move.b  (a0)+,d1
  217.                cmp.b   #'N',d1
  218.                beq.s   1$
  219.                cmp.b   #'n',d1
  220.                bne.s   2$
  221. 1$:
  222.       ;------ got a *N -- turn into a newline
  223.  
  224.                moveq   #10,d1
  225.                bra.s   addquotechar
  226. 2$:
  227.                cmp.b   #'E',d1
  228.                beq.s   3$
  229.                cmp.b   #'e',d1
  230.                bne.s   addquotechar
  231. 3$:
  232.       ;------ got a *E -- turn into a escape
  233.  
  234.                moveq   #27,d1
  235. addquotechar:
  236.                move.b  d1,(a2)+
  237.                bra.s   doquote
  238.  
  239. parmExit:
  240.  
  241.       ;------ all done -- null terminate the baby
  242.  
  243.                clr.b   (a2)
  244.                clr.l   (a3)
  245.  
  246.       ;------ compute the # of arguments
  247.  
  248.                move.l   #__dosCmdBuf,d0
  249.                sub.l   a3,d0
  250.                not.l   d0
  251.                lsr.l   #2,d0
  252.  
  253.                movem.l   (sp)+,a2/a3
  254.                pea   argvArray         ; *argv[]
  255.                move.l   d0,-(sp)       ;  argc
  256.  
  257. *
  258. *  The above code relies on the end of line containing a control
  259. *  character of any type, i.e. a valid character must not be the
  260. *  last.  This fact is ensured by DOS.
  261. *
  262.       ;------ get standard input handle:
  263.  
  264.                jsr     _Input
  265.                move.l  d0,__fh_stdin
  266.                move.l  d0,_stdin
  267.  
  268.       ;------ get standard output handle:
  269.  
  270.                jsr     _Output
  271.                move.l  d0,__fh_stdout
  272.                move.l  d0,__fh_stderr
  273.  
  274.       ;------ update the shadow variables
  275.  
  276.                move.l  d0,_stdout
  277.                move.l  d0,_stderr
  278.  
  279.       ;------ call C main entry point
  280.  
  281.                jsr   __main
  282.  
  283.       ;------ return success code: THIS CODE SHOULD NEVER EXECUTE
  284.  
  285.                moveq.l #0,D0
  286.                move.l  __initialSP,sp   ; restore stack ptr
  287.                rts
  288.  
  289. ;=======================================================================
  290. ;====== Workbench Startup Code =========================================
  291. ;=======================================================================
  292.  
  293. fromWorkbench:
  294.  
  295.       ;------ open the DOS library:
  296.  
  297.                bsr     openDOS
  298.  
  299.       ;------ we are now set up.  wait for a message from our starter
  300.  
  301.                bsr     waitmsg
  302.  
  303.       ;------ save the message so we can return it later
  304.  
  305.                move.l  d0,_WBenchMsg
  306.  
  307.       ;------ push the message on the stack for wbmain
  308.  
  309.                move.l  d0,-(SP)
  310.                clr.l   -(SP)      indicate: run from Workbench
  311.  
  312.       ;------ get the first argument
  313.  
  314.                move.l  d0,a2
  315.                move.l  sm_ArgList(a2),d0
  316.                beq.s   docons
  317.  
  318.       ;------ and set the current directory to the same directory
  319.  
  320.                move.l   _DOSBase,a6
  321.                move.l   d0,a0
  322.                move.l   wa_Lock(a0),d1
  323.                callsys  CurrentDir
  324.  
  325. * ==============================================
  326. * ============ Opens stdio window ==============
  327. * ==============================================
  328.  
  329. docons:
  330.                lea.l    _TWspec,a0
  331.                cmp.b    #0,(a0)         * No window if TWspec is null string
  332.                beq.s    domain
  333.  
  334.    ;------ open up the file
  335.  
  336.                move.l   a0,d1
  337.                move.l   #MODE_OLDFILE,d2
  338.                move.l   _DOSBase,a6
  339.                callsys  Open
  340.  
  341.    ;------ set the C input and output descriptors
  342.  
  343.                cmp.l    #0,d0
  344.                beq.s    exit2
  345.                move.l   d0,__fh_stdin
  346.                move.l   d0,__fh_stdout
  347.                move.l   d0,__fh_stderr
  348.  
  349.    ;------ set the shadow variables
  350.  
  351.                move.l   d0,_stdin
  352.                move.l   d0,_stdout
  353.                move.l   d0,_stderr
  354.  
  355.    ;------ set the console task (so Open( "*", mode ) will work
  356.    ;   waitmsg has left the task pointer in A4 for us
  357.  
  358.                move.l  d0,pr_CIS(A4)
  359.                move.l  d0,pr_COS(A4)
  360.                lsl.l   #2,d0
  361.                move.l  d0,a0
  362.                move.l  fh_Type(a0),pr_ConsoleTask(A4)
  363. domain:
  364.                jsr     __main
  365.                moveq.l #0,d0           ; Successful return code
  366.                bra.s   exit2
  367.  
  368.  
  369. ************************************************************************
  370. *
  371. *   C Program _exit Function
  372. *
  373. ************************************************************************
  374. *
  375. *  Warning: this function really needs to do more than this.
  376. *
  377. ************************************************************************
  378.  
  379. __exit:
  380.                move.l  4(SP),d0   ; extract return code
  381. exit2:
  382.  
  383.    ;------ Close ToolWin if we have one:
  384.  
  385.                tst.l   _WBenchMsg
  386.                beq.s   noTW
  387.                move.l  __fh_stdin,d1
  388.                bmi.s   noTW
  389.                move.l  _DOSBase,a6
  390.                callsys Close
  391. noTW:
  392.                move.l  __initialSP,SP   ; restore stack pointer
  393.                move.l  d0,-(SP)   ; save return code
  394.  
  395.       ;------ close DOS library:
  396.  
  397.                move.l  4,A6
  398.                move.l  _DOSBase,d0
  399.                beq.s   1$
  400.                move.l  d0,a1
  401.                callsys CloseLibrary
  402. 1$
  403.       ;------ if we ran from CLI, skip workbench cleanup:
  404.  
  405.                tst.l   _WBenchMsg
  406.                beq.s   exitToDOS
  407.  
  408.       ;------ return the startup message to our parent
  409.       ;------   we forbid so workbench can't UnLoadSeg() us
  410.       ;------   before we are done:
  411.  
  412.                callsys Forbid
  413.                move.l  _WBenchMsg,a1
  414.                callsys ReplyMsg
  415.  
  416.       ;------ this rts sends us back to DOS:
  417.  
  418. exitToDOS:
  419.                move.l  (SP)+,d0
  420.                rts
  421.  
  422.  
  423. ;-----------------------------------------------------------------------
  424. noDOS:
  425.                ALERT   (AG_OpenLib!AO_DOSLib)
  426.                moveq.l #100,d0
  427.                bra     exit2
  428.  
  429. ;-----------------------------------------------------------------------
  430. ; This routine gets the message that workbench will send to us
  431. ; called with task id in A4
  432.  
  433. waitmsg:
  434.                lea     pr_MsgPort(A4),a0     * our process base
  435.                callsys WaitPort
  436.                lea     pr_MsgPort(A4),a0     * our process base
  437.                callsys GetMsg
  438.                rts
  439.  
  440. ;-----------------------------------------------------------------------
  441. ;  Open the DOS library:
  442.  
  443. openDOS:
  444.                lea     DOSName(pc),A1
  445.                moveq   #0,d0
  446.                callsys OpenLibrary
  447.                move.l  D0,_DOSBase
  448.                beq     noDOS
  449.  
  450.                rts
  451.  
  452. DOSName        dc.b    'dos.library',0
  453.  
  454. ************************************************************************
  455.  
  456.                SECTION 2,DATA
  457.  
  458. ************************************************************************
  459.  
  460. VerRev         dc.w    33,1
  461.  
  462. _SysBase       dc.l    0
  463. _DOSBase       dc.l    0
  464. _errno         dc.l    0
  465. __fh_stdin     dc.l    -1              ; BPTR to AmigaDOS file
  466. __fh_stdout    dc.l    -1              ; BPTR to AmigaDOS file
  467. __fh_stderr    dc.l    -1              ; BPTR to AmigaDOS file
  468.  
  469. _stdin         dc.l    -1              ; Shadow variable for amiga.lib
  470. _stdout        dc.l    -1              ; Shadow variable for amiga.lib
  471. _stderr        dc.l    -1              ; Shadow variable for amiga.lib
  472.  
  473. __initialSP      dc.l    0
  474. _WBenchMsg     dc.l    0
  475.  
  476. __dosCmdLen      dc.l    0
  477. __dosCmdBuf      dc.l    0
  478.  
  479. argvArray      ds.l    32              ; Maximum of 32 arguments
  480. argvBuffer     ds.b    256             ; Maximum sum of argument lengths
  481.  
  482.                END
  483.  
  484.